package com.goodwill.androidtest.handlertest;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.LayoutTransition;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.util.TimeUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.goodwill.androidtest.R;

import java.util.Date;

public class HandlerTestActivity extends AppCompatActivity {

    private TextView mTvResult;
    private Button btn_click;
    private Handler aHandler;
    //    private TestThread mTestThread;
    LayoutTransition transition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_test);
        initView();
    }

    boolean isSending = false;

    private void initView() {
        A a = new A();
        Thread thread = new Thread(a);
        thread.start();

        mTvResult = findViewById(R.id.tv_result);
        btn_click = findViewById(R.id.btn_click);
        btn_click.setOnClickListener((view) -> {
            new Thread(new B()).start();
//            mTestThread = new TestThread();
//            mTestThread.start();
        });
    }

//    Handler handler = new Handler(Looper.myLooper()) {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case 1:
//                    String result = (String) msg.obj;
//                    mTvResult.setText(result);
//                    break;
//                default:
//                    break;
//            }
//        }
//    };

//    class TestThread extends Thread {
//        @Override
//        public void run() {
//            super.run();
//
//            Looper.prepare();
//
//            for (int i = 0; i < 10; i++) {
//                try {
//                    Thread.sleep(2000);
//                    Message message = handler.obtainMessage();
//                    Message msg = Message.obtain();
//                    msg.what = 1;
//                    msg.obj = String.valueOf(System.currentTimeMillis());
//                    handler.sendMessage(msg);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//
////            Looper.loop();
//        }
//    }

    class A implements Runnable {

        @Override
        public void run() {
            Looper.prepare();
            aHandler = new Handler(Looper.myLooper()) {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    String tag_name = Thread.currentThread().getName();
                    Log.d(tag_name, "handleMessage: " + msg.obj);
                }
            };
            Looper.loop();
        }
    }

    class B implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                try {
                    Thread.sleep(2000);
                    Message msg = aHandler.obtainMessage();
                    msg.what = 1;
                    msg.obj = Thread.currentThread().getName() + "--" + String.valueOf(System.currentTimeMillis());
                    aHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
